/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flan.stock.view;

import com.flan.stock.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SimpleCommonTitleView extends LinearLayout{
	
	private Button leftButton,rightButton;
	private TextView titleTv;
	
	private String titleText,leftText,RightText;
	private boolean leftButVisable,rightButVisable;
	private int leftDrawableId,rightDrawableId;
	
	private Context context;

	public SimpleCommonTitleView(Context context) {
		super(context,null);
	}

	public SimpleCommonTitleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		this.context = context;
		
		//获取控件中自定义属性值
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.simpleCommonTitle);
		
		titleText = typedArray.getString(R.styleable.simpleCommonTitle_title_text);
		leftText = typedArray.getString(R.styleable.simpleCommonTitle_left_but_text);
		RightText = typedArray.getString(R.styleable.simpleCommonTitle_right_but_text);
		
		leftButVisable = typedArray.getBoolean(R.styleable.simpleCommonTitle_left_but_visable, false);
		rightButVisable = typedArray.getBoolean(R.styleable.simpleCommonTitle_right_but_visable, false);
		
		leftDrawableId = typedArray.getResourceId(R.styleable.simpleCommonTitle_left_but_drawable, -1);
		rightDrawableId = typedArray.getResourceId(R.styleable.simpleCommonTitle_right_but_drawable, -1);
		
		typedArray.recycle();
		
		//获取组合控件中的各个子控件
		inflate(context, R.layout.layout_simple_common_title, this);
		leftButton = (Button) this.findViewById(R.id.title_left_but);
		rightButton = (Button) this.findViewById(R.id.title_right_but);
		titleTv = (TextView) this.findViewById(R.id.title_tv);
		
		titleTv.setText(titleText);
		leftButton.setText(leftText);
		rightButton.setText(RightText);
		
		if(leftButVisable){
			leftButton.setVisibility(VISIBLE);
		}
		
		if(rightButVisable){
			rightButton.setVisibility(VISIBLE);
		}
		
		if(leftDrawableId != -1){
			leftButton.setBackgroundResource(leftDrawableId);
		}
		
		if(rightDrawableId != -1){
			rightButton.setBackgroundResource(rightDrawableId);
		}
		
	}
	
	/**
	 * 设置左侧按钮的监听器
	 * @param listener
	 */
	public void setOnLeftClickListener(OnClickListener listener){
		leftButton.setOnClickListener(listener);
	}
	
	/**
	 * 设置右侧按钮的监听器
	 * @param listener
	 */
	public void setOnRightClickListener(OnClickListener listener){
		rightButton.setOnClickListener(listener);
	}
	
	/**
	 * 通过字符串资源设置title的文字内容
	 * @param resId
	 */
	public void setTitleText(int resId){
		titleTv.setText(context.getString(resId));
	}
	
	public void setTitleText(String text){
		titleTv.setText(text);
	}
	
}

















